package com.ruoyi.web.controller.wq;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.wq.domain.WqActivity;
import com.ruoyi.wq.domain.WqActivityUser;
import com.ruoyi.wq.domain.vo.WqActivityVO;
import com.ruoyi.wq.service.IWqActivityService;
import com.ruoyi.wq.service.IWqActivityUserService;

import cn.hutool.core.date.DateUtil;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.service.ActivityService;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 活动详情Controller
 * 
 * @author ruoyi
 * @date 2023-08-12
 */
@RestController
@RequestMapping("/wq/activity")
public class WqActivityController extends BaseController {

    @Autowired
    private IWqActivityService wqActivityService;
    
    @Autowired
    private ActivityService activityService;
    
    @Autowired
    private IWqActivityUserService wqActivityUserService;
    
    @Autowired
    private ISysUserService sysUserService;

    /**
     * 查询活动列表
     */
    @Anonymous
    @GetMapping("/list")
    public TableDataInfo list(WqActivity wqActivity){
        TableDataInfo info = wqActivityService.getPageList(wqActivity);
        return info;
    }
    
    /**
     * 派对管理列表
     */
    @GetMapping("/getActivityList")
    public TableDataInfo getActivityList(WqActivity wqActivity){
        TableDataInfo info = wqActivityService.getPageActivityList(wqActivity);
        return info;
    }

    /**
     * 导出活动详情列表
     */
    @PreAuthorize("@ss.hasPermi('wq:activity:export')")
    @Log(title = "活动详情", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, WqActivity wqActivity)
    {
        List<WqActivity> list = wqActivityService.selectWqActivityList(wqActivity);
        ExcelUtil<WqActivity> util = new ExcelUtil<WqActivity>(WqActivity.class);
        util.exportExcel(response, list, "活动详情数据");
    }
    
    /**
     * 获取活动详细信息
     */
    @Anonymous
    @GetMapping(value = "/getDetail")
    public AjaxResult getDetail(String id){
    	AjaxResult ajax = AjaxResult.success();
    	WqActivity activity = wqActivityService.getById(id);
    	WqActivityVO vo = new WqActivityVO();
    	BeanUtils.copyProperties(activity, vo, WqActivityVO.class);
    	if(activity.getStartTime() != null && activity.getEndTime() != null){
    		vo.setApplyTime(DateUtils.getRangeTime(activity.getStartTime(), activity.getEndTime()));
    	}
    	SysUser user = sysUserService.getById(activity.getUserId());
    	List<SysUser> userList = wqActivityUserService.getUserListByActivityId(id);
    	ajax.put("info", vo);
    	ajax.put("user", user);
    	ajax.put("userList", userList);
        return ajax;
    }
    
    /**
     * 获取活动详细信息
     */
    @Anonymous
    @GetMapping(value = "/getApplyTimeList")
    public AjaxResult getApplyTimeList(String id){
    	AjaxResult ajax = AjaxResult.success();
    	if(StringUtils.isNotBlank(id)){
    		WqActivity activity = wqActivityService.getById(id);
    		List<String> appointTime = new ArrayList<String>();
        	appointTime.add(DateUtil.formatDateTime(activity.getStartTime()));
        	appointTime.add(DateUtil.formatDateTime(activity.getEndTime()));
        	ajax.put("appointTime", appointTime);
    	}
    	
    	QueryWrapper<WqActivity> queryWrapper = new QueryWrapper<>();
    	WqActivity entity = new WqActivity();
    	entity.setStatus("1");
    	queryWrapper.setEntity(entity);
    	queryWrapper.in("activity_status", Arrays.asList("2","3"));
    	List<WqActivity> activities = wqActivityService.list(queryWrapper);
    	List<List<String>> timeList = new ArrayList<>();
    	for(WqActivity ac : activities){
    		List<String> time = new ArrayList<>();
    		time.add(DateUtil.formatDateTime(ac.getStartTime()));
    		time.add(DateUtil.formatDateTime(ac.getEndTime()));
    		timeList.add(time);
    	}
    	ajax.put("timeList", timeList);
        return ajax;
    }
    
    /**
     * 获取活动发起人信息
     */
    @GetMapping(value = "/getUser")
    public AjaxResult getUser(String id){
    	AjaxResult ajax = AjaxResult.success();
    	WqActivity activity = wqActivityService.getById(id);
    	QueryWrapper<WqActivityUser> qw = new QueryWrapper<>();
    	WqActivityUser entity = new WqActivityUser();
    	entity.setActivityId(id);
    	entity.setStatus("1");
    	entity.setUserId(SecurityUtils.getUserId());
    	qw.setEntity(entity);
    	WqActivityUser activityUser = wqActivityUserService.getOne(qw);
    	if(activityUser == null){
    		throw new ServiceException("报名后可查看");
    	}
    	SysUser user = sysUserService.getById(activity.getUserId());
    	ajax.put("user", user);
        return ajax;
    }

    /**
     * 获取活动详情详细信息
     */
    @PreAuthorize("@ss.hasPermi('wq:activity:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return AjaxResult.success(wqActivityService.selectWqActivityById(id));
    }

    /**
     * 创建活动
     */
    @PostMapping("/add")
    public AjaxResult add(@RequestBody WqActivity wqActivity){
        return AjaxResult.success(activityService.saveActivity(wqActivity));
    }
    
    /**
     * 报名活动
     */
    @PostMapping("/join")
    public AjaxResult join(@RequestBody WqActivity wqActivity){
        return AjaxResult.success(activityService.saveJoinActivity(wqActivity));
    }
    
    /**
     * 取消报名
     */
    @PostMapping("/out")
    public AjaxResult out(@RequestBody WqActivity wqActivity){
        return AjaxResult.success(activityService.saveOutActivity(wqActivity));
    }
    
    /**
     * 预约时间
     */
    @PostMapping("/applyTime")
    public AjaxResult applyTime(@RequestBody WqActivity wqActivity){
        return AjaxResult.success(activityService.saveApplyTimeActivity(wqActivity));
    }
    
    /**
     * 确认到店
     */
    @PostMapping("/signTime")
    public AjaxResult signTime(@RequestBody WqActivity wqActivity){
        return AjaxResult.success(activityService.saveSignTimeActivity(wqActivity));
    }

    /**
     * 修改活动详情
     */
    @PreAuthorize("@ss.hasPermi('wq:activity:edit')")
    @Log(title = "活动详情", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WqActivity wqActivity)
    {
        return toAjax(wqActivityService.updateWqActivity(wqActivity));
    }

    /**
     * 删除活动详情
     */
    @PreAuthorize("@ss.hasPermi('wq:activity:remove')")
    @Log(title = "活动详情", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(wqActivityService.deleteWqActivityByIds(ids));
    }
}
